/**************************************************************************************
* MessagePanel.java
* 
*************************************************************************************/
package creid.mythos.ui;

import java.awt.Color;
import java.awt.GridLayout;

import javax.swing.JTextField;
import javax.swing.SwingConstants;

public class MessagePanel extends GamePanel
{
	private static final long serialVersionUID = -430155379672379420L;

	//Constants/////////////////////////////////////////////////////////////////////////
	public static final int NUM_MESSAGES = 4;
	
	public static final int MESSAGE_PANEL_HEIGHT = NUM_MESSAGES * GameUI.FONT_VERTICAL_SPACING;

	//Attributes////////////////////////////////////////////////////////////////////////

	//Stores up to four lines of messages.
	private JTextField[] messages;
	
	//Constructors//////////////////////////////////////////////////////////////////////
	
	public MessagePanel()
	{
		messages = new JTextField[NUM_MESSAGES];
		
		setLayout(new GridLayout(4, 1));
		for (int i = 0; i < messages.length; i++)
		{
			messages[i] = formattedTextField(SwingConstants.LEFT);
			add(messages[i]);
		}
	}

	//Methods///////////////////////////////////////////////////////////////////////////
	//-----------------------------------------------------------------
	//writeMessage
	//-----------------------------------------------------------------		
	
	void writeMessage(String msg)
	{
		//Roll the older messages
		for (int i = 0; i < NUM_MESSAGES - 1; i++)
		{
			messages[i].setText(messages[i + 1].getText());
			
			//Fade the brightness of older messages to indicate we go from
			//oldest to newest
			Color old = messages[i + 1].getForeground();
			Color c = new Color(Math.max(0, old.getRed() - 50), 
					Math.max(0, old.getGreen() - 50), 
					Math.max(0, old.getBlue() - 50));
			messages[i].setForeground(c);
		}
		
		JTextField f = messages[NUM_MESSAGES - 1]; 
		f.setText(" " + msg);
		f.setForeground(Color.WHITE);
	}
	
	//-----------------------------------------------------------------
	//writeTransientMessage
	//-----------------------------------------------------------------		
	
	void writeTransientMessage(String msg)
	{
		//We do not roll the older messages
		
		messages[NUM_MESSAGES - 1].setText(" " + msg);
		messages[NUM_MESSAGES - 1].setForeground(GameUI.colors.get(ColorData.WHITE));
	}
	
	void overwriteLastMessage(String message)
	{
		messages[NUM_MESSAGES - 1].setText(" " + message);
	}
	
	void clearMessages()
	{
		for (int i = 0; i < NUM_MESSAGES; i++)
			messages[i].setText("");
	}
}
